{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0c63cd89",
   "metadata": {},
   "outputs": [],
   "source": [
    "from OpticalSensor import CA410Sensor as EyeSensor\n",
    "import time\n",
    "\n",
    "import jtoollib as jt\n",
    "from pic_window import *\n",
    "\n",
    "import math\n",
    "import pandas as pd\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b013ee59",
   "metadata": {},
   "source": [
    "# NT71877"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "78884e70",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 目标gamma\n",
    "gamma_target = 2.2\n",
    "\n",
    "# 1. 实例化传感器对象（根据实际串口号和波特率修改）\n",
    "sensor = EyeSensor(port='COM8', baudrate=115200)\n",
    "sensor.XYZmode()\n",
    "\n",
    "# 扫描JI2C\n",
    "devices = jt.scan_devices_sn()\n",
    "# 开启JI2C\n",
    "handle = jt.open_device(devices[0])\n",
    "\n",
    "L255_Y = []\n",
    "L0_Y = []\n",
    "\n",
    "try:\n",
    "    # 1) 启动后立即显示一张空图，然后动态更新\n",
    "    win = NonBlockingImageWindow(\"Demo\", size=(800, 800), fps=10)\n",
    "\n",
    "    # RGB数据 BGR格式\n",
    "    img = np.zeros((800, 800, 3), dtype=np.uint8)\n",
    "    img[:, :, :] = 0\n",
    "    win.update(img, fmt=\"rgb\")\n",
    "    \n",
    "    for i in range(0,32,1):\n",
    "        jt.i2c_write(handle=handle, addr=0xC2, reg=0x34, data=[0xD8,i])\n",
    "        jt.i2c_write(handle=handle, addr=0xC2, reg=0x34, data=[0xD9,i])\n",
    "        \n",
    "        img = np.zeros((800, 800, 3), dtype=np.uint8)\n",
    "        img[:, :, :] = 0\n",
    "        win.update(img, fmt=\"rgb\")\n",
    "\n",
    "        time.sleep(0.2)\n",
    "        flag = 1\n",
    "        while(flag):\n",
    "            try:\n",
    "                __, y_temp, __ = sensor.GetXYZ()\n",
    "                flag = 0\n",
    "            except:\n",
    "                flag = 1\n",
    "        L0_Y.append(y_temp)\n",
    "\n",
    "        img = np.zeros((800, 800, 3), dtype=np.uint8)\n",
    "        img[:, :, :] = 255\n",
    "        win.update(img, fmt=\"rgb\")\n",
    "\n",
    "        time.sleep(0.2)\n",
    "        flag = 1\n",
    "        while(flag):\n",
    "            try:\n",
    "                __, y_temp, __ = sensor.GetXYZ()\n",
    "                flag = 0\n",
    "            except:\n",
    "                flag = 1\n",
    "        L255_Y.append(y_temp)\n",
    "\n",
    "finally:\n",
    "    jt.jtool.DevClose(handle)\n",
    "    win.stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2d2c257b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5098\n",
      "0.5102\n",
      "0.5099\n",
      "0.5103\n",
      "0.5098\n",
      "0.5098\n",
      "0.5099\n",
      "0.5099\n",
      "0.5094\n",
      "0.5098\n",
      "0.5098\n",
      "0.5101\n",
      "0.5102\n",
      "0.5102\n",
      "0.5106\n",
      "0.5111\n",
      "0.511\n",
      "0.5113\n",
      "0.5117\n",
      "0.5122\n",
      "0.5127\n",
      "0.513\n",
      "0.5138\n",
      "0.5147\n",
      "0.5149\n",
      "0.5159\n",
      "0.5165\n",
      "0.5178\n",
      "0.5183\n",
      "0.5194\n",
      "0.5208\n",
      "0.5226\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(L0_Y)):\n",
    "    print(L0_Y[i])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "163265ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
